<?
require('../bootstrap.php');
require('../includes/site_config.php');
auth_redirect();

// Load config
$post = $db->get("SELECT * FROM $config_table");
// Frontpage options
$pages = $db->get_result("SELECT name, title FROM $pages_table ORDER BY `index` ASC");

// Load templates
require('../includes/Template.class.php');
require('../includes/TemplateSystem.class.php');
$ts = new TemplateSystem();
try {
	$ts->init('../templates');
} catch (Exception $e) {
	die($e->getMessage());
}

// Array containing field errors
$errors = array();

if (isset($_POST['submit'])) {
	$post = escape_array($_POST);
	
	if ($post['site_name'] == '') {
		$errors['site_name'] = "Sidens navn skal udfyldes";
	}
	if ($post['site_index'] == '') {
		$errors['site_index'] = "Forsidens navn skal udfyldes";
	}
	if ($post['template'] == '') {
		$errors['template'] = 'Design skal vælges';
	}
	
	if (count($errors) <= 0) {
		
		$q = "UPDATE $config_table SET site_name = '$post[site_name]', site_index = '$post[site_index]', template = '$post[template]'";

		if ($db->execute($q)) {
			$session->set('settings_saved', true);
			redirect("settings.php");
		} else {	
			// ?
		}		
	}
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<title>KomitCMS | Indstillinger</title>
		<meta http-equiv="content-type" content="text/html;charset=utf-8" />
		<meta http-equiv="Content-Style-Type" content="text/css" />
		<link rel="stylesheet" type="text/css" href="../stylesheet/style.css" />
		<script type="text/javascript" src="../javascript/prototype.js"></script>
	</head>
	<body>
		<div class="AUI_Header">
			<h1>Admin</h1>
			<? include 'menu.php' ?>
		</div>
		<div class="AUI_Content">
			<h2>Indstillinger</h2>
			
			<? if ($session->get('settings_saved') == true) { 
				$session->del('settings_saved');
				?>
				<div class="UI_Notice_OK">
					<p>Dine ændringer er gemt</p>
				</div>
			<? } ?>
			
			<? if (count($errors) > 0) { ?>
				<div class="UI_Errors">
					<ul>
						<? foreach ($errors as $e) { ?>
							<li><? echo $e ?></li>
						<? } ?>
					</ul>
				</div>
			<? } ?>

			<form method="post">
			<table class="UI_Editor" cellspacing="0" cellpadding="0">
				<tr>
					<td class="label">Sidens navn:<br/>
						<small>(Obligatorisk)</small></td>
					<td><input type="text" name="site_name" value="<? echo $post['site_name'] ?>"
						style="<? echo ($errors['site_name']) ? 'border: 1px solid red;' : '' ?>"/></td>
				</tr>
				<tr>
					<td class="label">
						Forside:<br/>
						<small>(Obligatorisk)</small>					
					</td>
					<td>
						<select name="site_index" style="<? echo ($errors['site_index']) ? 
							'border: 1px solid red;' : '' ?>"  />

							<? if (count($pages) > 0) { ?>
								<? foreach ($pages as $p) { ?>
									<option value="<? echo $p['name'] ?>" 
										<? if ($p['name'] == $post['site_index']) echo 'selected="selected"' ?>>
										<? echo $p['title'] ?>
									</option>
								<? } ?>
							<? } ?>
						</select>
				</tr>
				<tr>
					<td class="label">
						Design:<br/>
						<small>(Obligatorisk)</small>
					</td>
					<td>
						<select name="template"	style="<?= ($errors['template']) ? 'border: 1xp solid red' : '' ?>">
							<? if (count($ts->getTemplates()) > 0) { ?>
								<? foreach ($ts->getTemplates() as $t) { ?>
									<option value="<?= $t->name ?>" 
										<?= ($t->name == $post['template'] || $t->name == '') ? 'selected="selected"' : '' ?>>
										<?= $t->name ?>
									</option>
								<? } ?>
							<? } ?>
						</select>
					</td>
				</tr>
				<tr>
					<td><br /><br /></td>
				</tr>
				<!--
				<tr>
					<td class="label">
						Sidehovedets farve:<br/>
						<small>(Hex-værdi)</small>
					</td>
					<td><input type="text" name="header_color" value="<? echo $post['style_header_color'] ?>" 
						style="<? echo ($errors['header']) ? 'border: 1px solid red;' : '' ?>"/></td>
				</tr>
				<tr>
					<td class="label">
						Menuens farve:<br/>
						<small>(Hex-værdi)</small>
					</td>
					<td><input type="text" name="menu_color" value="<? echo $post['style_menu_color'] ?>" 
						style="<? echo ($errors['menu_color']) ? 'border: 1px solid red;' : '' ?>"/></td>
				</tr>
			-->
				<tr>
					<td />
					<td>
						<div class="divider" />
					</td>
				</tr>
				<tr>
					<td colspan="2">
						<div class="buttons">
							<input type="submit" name="submit" value="Gem">
							<input type="button" value="Annuller" onclick="document.location.href='pages.php'"/>
						</div>
					</td>
				</tr>
			</table>
			</form>
			</div>
		</div>
	</body>
</html>